using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class ImageUtilities
{
    internal static Image ScalePhotoNoBackground(Image image, int width, int height, Color backgroundColor)
    {
        int sourceWidth = image.Width;
        int sourceHeight = image.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)width / (float)sourceWidth);
        nPercentH = ((float)height / (float)sourceHeight);
        nPercent = nPercentW;
        nPercent = (nPercent > 1 ? 1 : nPercent);

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(backgroundColor);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(image,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return bmPhoto;
    }
    
    internal static Image ScalePhoto(Image image, int width, int height, Color backgroundColor)
    {
        int sourceWidth = image.Width;
        int sourceHeight = image.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)width / (float)sourceWidth);
        nPercentH = ((float)height / (float)sourceHeight);
        nPercent = Math.Min(nPercentW, nPercentH);
        nPercent = (nPercent > 1 ? 1 : nPercent);

        if (nPercent == 1)
        {
            destX = System.Convert.ToInt16((width - (sourceWidth * nPercent)) / 2);
            destY = System.Convert.ToInt16((height - (sourceHeight * nPercent)) / 2);
        }
        else if (nPercentH < nPercentW)
        {
            destX = System.Convert.ToInt16((width - (sourceWidth * nPercent)) / 2);
        }
        else
        {
            destY = System.Convert.ToInt16((height - (sourceHeight * nPercent)) / 2);
        }

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(backgroundColor);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(image,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return bmPhoto;
    }
    
    internal static Image SetBackground(Color backgroundColor, Image image, int width, int height)
    {
        Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(backgroundColor);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        int destWidth = image.Width;
        int destHeight = image.Height;
        int destX = (width - image.Width) / 2;
        int destY = (height - image.Height) / 2;

        grPhoto.DrawImage(image,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(0, 0, image.Width, image.Height),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return bmPhoto;
    }

    public static Image ProcessPhoto(Image image, int width, int height)
    {
        Color bgcolor = Color.White;
        image = ScalePhoto(image, width, height, bgcolor);
        image = SetBackground(bgcolor, image, width, height);
        return image;
    }

    public static Image ProcessPhotoNoBackground(Image image, int width, int height)
    {
        Color bgcolor = Color.White;
        image = ScalePhotoNoBackground(image, width, height, bgcolor);        
        return image;
    }

    public static void SavePhoto(Image image, string fileName)
    {
        image.Save(fileName, ImageFormat.Jpeg);
        image.Dispose();
    }

    public static void MakeThumbnailPhoto(Image originalImage, int thumbnailPhotoWidth, int thumbnailPhotoHeight, 
        string thumbnailPhotoFolderPath, string photoFileName)
    {
        FileUtilities.CreateDirectory(thumbnailPhotoFolderPath);

        Image thumbnailPhoto = ImageUtilities.ProcessPhoto(originalImage, thumbnailPhotoWidth, thumbnailPhotoHeight);
        SavePhoto(thumbnailPhoto, Path.Combine(thumbnailPhotoFolderPath, photoFileName));
    }

    public static void ResizePhotoNoBackground(Image originalImage, int photoWidth, int photoHeight,
        string photoFolderPath, string photoFileName)
    {
        FileUtilities.CreateDirectory(photoFolderPath);

        Image photo = ImageUtilities.ProcessPhotoNoBackground(originalImage, photoWidth, photoHeight);
        SavePhoto(photo, Path.Combine(photoFolderPath, photoFileName));
    }
}

